google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(getRows);

function drawChart(rows) {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Aika');
    data.addColumn('number', 'Alcohol');
    data.addRows(rows);

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, {width: 400, height: 240, title: 'Company Performance'});
}

function getRows() {
    $.get('data', dataReceived);
}

function dataReceived(data) {
    var rows = data.split('\n');

    var datarows = [];

    var firstskipped = false;
    for (i in rows) {
        if (!firstskipped) {
          firstskipped = true;
          continue;
        }
        var row = rows[i];

        var columns = row.split(',');

        var datarow = [columns[1], getDate(columns[0]), Math.floor(columns[2] * 100.0), Math.floor(columns[3] * 100.0)];

        datarows.push(datarow);
    }

    alert(datarows);

    drawChart(datarows);
}

function getDate(str) {
    var date = new Date(str);
    return date;

//    var minutes = Math.round((new Date().getTime() - date.getTime()) / 1000000);
//    alert(minutes);
//    var max = 2000;

//    var fake = max - minutes;

//    return new Date(fake, 0, 1);
}


